package com.ruoyi.bom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.bom.config.RequestDataHelper;
import com.ruoyi.bom.domain.TqlMaterialTransfer;
import com.ruoyi.bom.domain.TqlStapInChange;
import com.ruoyi.bom.dymapper.TqlStapInChangeMapper;
import com.ruoyi.bom.service.TqlStapInChangeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhengcl
 * @since 1.0.0
 */
@Service
public class TqlStapInChangeServiceImpl implements TqlStapInChangeService {

    @Autowired
    private TqlStapInChangeMapper tqlStapInChangeMapper;

    @Override
    public List<TqlStapInChange> selectList(Map<String, Object> params) {
        Calendar now = Calendar.getInstance();
        int month = now.get(Calendar.MONTH) + 1;

        String date = (String) params.get("date");
        if (StringUtils.isNotBlank(date)) {
            try {
                // 创建SimpleDateFormat对象，用于解析日期字符串
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                // 获取日期对象中的日部分
                month = sdf.parse(date).getMonth() +1;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        String createBy = (String) params.get("createBy");

        String fbillno = (String) params.get("fbillno");

        String fmaterialIdfnumber = (String) params.get("fmaterialIdfnumber");
        QueryWrapper<TqlStapInChange> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(createBy), "createBy", createBy);
        wrapper.eq(StringUtils.isNotBlank(fbillno), "fbillno", fbillno);
        wrapper.eq(StringUtils.isNotBlank(fmaterialIdfnumber), "FMaterialIdfnumber", fmaterialIdfnumber);
        wrapper.apply(StringUtils.isNotBlank(date),"date_format(FDate, '%Y-%m-%d') ='" + date + "'");

        int finalMonth = month;
        RequestDataHelper.setRequestData(new HashMap<String, Object>() {{
            put("index", finalMonth);
        }});
        List<TqlStapInChange> dtoList = tqlStapInChangeMapper.selectList(wrapper);

        return dtoList;
    }
}
